package com.cy.lib.image;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;

import com.nostra13.universalimageloader.core.assist.LoadedFrom;
import com.nostra13.universalimageloader.core.display.BitmapDisplayer;
import com.nostra13.universalimageloader.core.imageaware.ImageAware;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;

/**
 * @author CY
 * @date 2014-10-27
 */
public class CircleBitmapDisplayer implements BitmapDisplayer {

    public CircleBitmapDisplayer() {
    }

    @Override
    public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
//        if (!(imageAware instanceof ImageViewAware)) {
//            throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected.");
//        }

        if (imageAware != null && imageAware instanceof ImageViewAware && !bitmap.isRecycled())
            imageAware.setImageBitmap(generateCireleBitmap(bitmap));
    }

    /**
     * @author CY
     */
    public static class CircleDrawable extends Drawable {

        // 着色器
        BitmapShader mShader;
        Paint mPaint;
        Bitmap mBitmap;

        int radius = -1;

        public CircleDrawable(Bitmap bitmap) {

            int min = Math.min(bitmap.getWidth(), bitmap.getHeight());
            int x = (bitmap.getWidth() - min) >> 1;
            int y = (bitmap.getHeight() - min) >> 1;
            Bitmap subBitmap = Bitmap.createBitmap(bitmap, x, y, min, min);
            if (subBitmap != bitmap) {
                bitmap.recycle();
            }
            mBitmap = subBitmap;

            mShader = new BitmapShader(mBitmap, TileMode.CLAMP, TileMode.CLAMP);
            mPaint = new Paint();
            mPaint.setShader(mShader);
            mPaint.setAntiAlias(true);
            setRadius(min >> 1);
        }

        public void setRadius(int radius) {
            this.radius = radius;
        }

        @Override
        public void draw(Canvas canvas) {
//            int w = mBitmap.getWidth();
//            int h = mBitmap.getHeight();
//            if (radius == -1) {
//                radius = w > h ? h >> 1 : w >> 1;
//            }
            canvas.drawCircle(radius, radius, radius, mPaint);
//            mBitmap.recycle();
        }

        @Override
        public void setAlpha(int alpha) {
            // TODO Auto-generated method stub
            mPaint.setAlpha(alpha);
        }

        @Override
        public void setColorFilter(ColorFilter cf) {
            // TODO Auto-generated method stub
            mPaint.setColorFilter(cf);
        }

        @Override
        public int getOpacity() {
            return PixelFormat.TRANSLUCENT;
        }

    }

    private Bitmap circleBitmap(Bitmap b) {
        int w = b.getWidth();
        int h = b.getHeight();
        int radius = w > h ? h >> 1 : w >> 1;
        Bitmap bitmap = Bitmap.createBitmap(radius * 2, radius * 2, Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint p = new Paint();
        BitmapShader shader = new BitmapShader(b, TileMode.CLAMP, TileMode.CLAMP);
        p.setShader(shader);
        p.setAntiAlias(true);
//		canvas.drawARGB(0, 0, 0, 0);
        canvas.drawCircle(radius, radius, radius, p);
        canvas.save();
        b.recycle();
        return bitmap;
    }

    private Bitmap generateCireleBitmap(Bitmap bitmap) {
        int min = Math.min(bitmap.getWidth(), bitmap.getHeight());
        int x = (bitmap.getWidth() - min) >> 1;
        int y = (bitmap.getHeight() - min) >> 1;
        Bitmap subBitmap = Bitmap.createBitmap(bitmap, x, y, min, min);
//        if (subBitmap != bitmap) {
//            bitmap.recycle();
//        }

        int r = min >> 1;
        Bitmap circle = Bitmap.createBitmap(min, min, Config.ARGB_4444);
        Canvas canvas = new Canvas(circle);
        BitmapShader mShader = new BitmapShader(subBitmap, TileMode.CLAMP, TileMode.CLAMP);
        Paint mPaint = new Paint();
        mPaint.setShader(mShader);
        mPaint.setAntiAlias(true);
        canvas.drawCircle(r, r, r, mPaint);
        canvas.save();
        subBitmap.recycle();
//        if (subBitmap != bitmap) {
//            bitmap.recycle();
//        }
        return circle;
    }
}
